package com.lhl.jwt.service;

import com.lhl.jwt.domain.entity.SysUser;
import com.lhl.jwt.exception.JwtException;
import com.lhl.jwt.repository.UserRepository;
import com.lhl.jwt.security.JwtUser;
import com.lhl.jwt.security.service.JwtUserDetailsService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @className: com.lhl.jwt.service.JwtUserDetailsServiceImpl
 * @description: TODO 类描述
 * @author: king
 * @date: 2021-01-07 13:35
 **/
@Api(tags = "JwtUserDetailsServiceImpl", produces = "JwtUserDetailsServiceImpl")
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Transactional(timeout = 10)
public class JwtUserDetailsServiceImpl implements JwtUserDetailsService {
    private final UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = userRepository.findByUsername(username).orElseThrow(()->new JwtException("没找到对应的用户"));
        return new JwtUser(sysUser.getUsername(),sysUser.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList(sysUser.getRole()));
    }
}
